$$scope.submitter = {};
$$scope.submmitData = {};
var purchaseStepEntity = {
    "processInstanceId": $CertusJsonAjax.getProInstanceId(),
    "stepInstanceId": $CertusJsonAjax.getStepInstanceId(),
}
$$scope.submmitData.purchaseStepEntity = {
    "processInstanceId": $CertusJsonAjax.getProInstanceId(),
    "stepInstanceId": $CertusJsonAjax.getStepInstanceId(),
}
var bqArray = {};
var oldBqArray = {};

// 采购记录
var proc1Columns = [
    {
        id: 'commodityCateName',
        title: '类目',
        width: 73,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'commodityClassName',
        title: '类别',
        width: 73,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'name',
        title: '名称',
        width: 73,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'descAndSpec',
        title: '品名及规格',
        width: 200,
        type: 'string',
        columnClass: 'text-left',
    }, {
        id: 'quantity',
        title: '数量',
        width: 100,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'estimatedAmount',
        title: '预计金额',
        width: 150,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '￥' + value + '元';
            return content;
          }
    }, {
        id: 'remark',
        title: '备注',
        type: 'string',
        columnClass: 'text-left',
    }, {
        id: 'operation',
        title: '操作',
        width: 60,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '';
            content += '<button class="btn btn-xs btn-success" onclick="showNowQuote(\'' + record.biCode + '\')"><i class="icon-eye-open"></i>  查看</button>';
            content += '  ';
            return content;
        }
    }];

// 采购比价单信息
var pryslColumns = [
//     {
//     id: 'no',
//     title: '序号',
//     width: 50,
//     type: 'string',
//     columnClass: 'text-center',
// }, 
{
    id: 'supplierName',
    title: '供应商名称',
    width: 150,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'name',
    title: '名称',
    width: 150,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'specification',
    title: '规格(参数)',
    // width: 200,
    type: 'string',
    columnClass: 'text-left',
}, {
    id: 'unit',
    title: '单位',
    width: 54,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'recievedNum',
    title: '实收数量',
    width: 74,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'acceptanceResolve',
    title: '采购单位验收',
    width: 100,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        if (value) {
            if (value == 1) {
                return "合格"
            } else {
                return "不合格"
            }
        }
        return "";
    }
}, {
    id: 'itAcceptance',
    title: '验收单位验收',
    width: 100,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        if (value) {
            if (value == 1) {
                return "合格"
            } else {
                return "不合格"
            }
        }
        return "";
    }
}, {
    id: 'p8',
    title: '备注',
    type: 'string',
    columnClass: 'text-left',
    hideType: 'lg|md|sm|xs'
},{
    id: 'operation',
    title: '操作',
    width: 160,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '';
        content += '<button class="btn btn-xs btn-success" onclick="shupload(\'' + record.biCode + '\',\'' + record.bqCode + '\');"><i class="icon-plus"></i>  资产入库</button>';
        content += '  ';
        content += '<button class="btn btn-xs btn-primary" onclick="pDowload('+ record.id +')"><i class="icon-eye-open"></i>  验收单</button>';
        return content;
    }
}];

function  isDifferent(element){
    return element.bqCode == nowBqCode;
}
var nowBqCode;
function shupload(biCode,bqCode) {
    $CertusFormAjax.ajax("inventedPurchase", {bqCode: bqCode, biCode: biCode}, function (res) {
        //bqArray  oldBqArray
        if(res.data){
            ;
            var bqArray2 = bqArray[biCode];
            bqArray2 = bqArray2.filter(isDifferent);
            bqArray[biCode] = bqArray2;
            oldBqArray[biCode].push(res.data);
            belowGrid.exhibitDatas = bqArray[biCode];
            belowGrid.refresh(false);
            endGrid.exhibitDatas = endGrid[biCode];
            endGrid.refresh();
        }


    })
}
var belowGrid;
var aboveGrid;
var endGrid
var brCode;

function showAllQuote() {
    var tempArray = new Array();
    if (bqArray) {
        for (var j in bqArray) {
            tempArray = tempArray.concat(bqArray[j]);
        }
    }
    belowGrid.exhibitDatas = tempArray;
    belowGrid.refresh(false);

    var oldTempArray = new Array();
    if (oldBqArray) {
        for (var j in oldBqArray) {
            oldTempArray = oldTempArray.concat(oldBqArray[j]);
        }
    }
    endGrid.exhibitDatas = oldTempArray;
    endGrid.refresh(false);

    $(".hdlog").show();
    $(".hdgbuy").show();
}
$CertusJsonAjax.ajax("purchaseBaseInfoAndQuoteHaveAgreementInventory", {
    "processInstanceId": $CertusJsonAjax.getProInstanceId(),
    "stepInstanceId": $CertusJsonAjax.getStepInstanceId()
}, function (res) {
    if (res.data) {
        ;
        $$scope.headerMap = res.data.headerMap;
        brCode = res.data.headerMap.brCode;
        // purchaseStepEntity.buyingRequisitionCode = res.data.headerMap.brCode;
        $$scope.submmitData.purchaseStepEntity.buyingRequisitionCode = res.data.headerMap.brCode;

        $$scope.submmitData.purchaseStepEntity.projId = res.data.headerMap.projId;
        var datas = res.data.buyings;
        var priceOption1 = {
            lang: 'zh-cn',
            ajaxLoad: false,
            extraWidth: 20,
            // loadURL:'url',
            exportFileName: '采购单条目信息',
            datas: datas,
            columns: proc1Columns,
            gridContainer: 'pslContainer',
        };
        aboveGrid = $.fn.dlshouwen.grid.init(priceOption1);
        aboveGrid.load();

        //初始化 报价 文件 数据
        if (datas) {
            for (var i in datas) {
                if (datas[i].biCode) {
                    bqArray[datas[i].biCode] = datas[i].buyingQuoteItemEntities;
                }
            }
            //初始化 历史报价 文件 数据
            for (var i in datas) {
                if (datas[i].biCode) {
                    oldBqArray[datas[i].biCode] = datas[i].inventorQuoteItemEntities;
                    ;
                }
            }
        }

        var prcListOption = {
            lang: 'zh-cn',
            ajaxLoad: false,
            extraWidth: 20,
            // loadURL:'url',
            check : true,
            checkWidth : 20,
            exportFileName: '供应商列表',
            datas: [],
            columns: pryslColumns,
            gridContainer: 'gyslContainer',
            tools: '',
            pageSize: 10,
            pageSizeLimit: [10, 15, 20]
        };
        belowGrid = $.fn.dlshouwen.grid.init(prcListOption);
        belowGrid.load();

        var endOption = {
            lang: 'zh-cn',
            ajaxLoad: false,
            extraWidth: 20,
            // loadURL:'url',
            exportFileName: '结果清单',
            datas: [],
            columns: endColumns,
            gridContainer: 'library04Container',
            // toolbarContainer: 'endToolBarContainer',
            tools: '',
            pageSize: 10,
            pageSizeLimit: [10, 15, 20]
        };
        endGrid = $.fn.dlshouwen.grid.init(endOption);
        endGrid.load();
    }
});

function showNowQuote(biCode) {
    if (biCode) {
        belowGrid.exhibitDatas = bqArray[biCode];
        belowGrid.refresh(false);

        endGrid.exhibitDatas = oldBqArray[biCode];
        endGrid.refresh();

        $(".hdlog").show();
        $(".hdgbuy").show();
    }
}


// 结果清单
var endColumns = [
//     {
//     id: 'number',
//     title: '序号',
//     width: 45,
//     type: 'string',
//     columnClass: 'text-center',
// }, 
{
    id: 'commodityCateName',
    title: '资产类目',
    width: 73,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'commodityClassName',
    title: '资产类别',
    width: 73,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'assetId',
    title: '资产编号',
    width: 170,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'descAndSpec',
    title: '资产描述',
    type: 'string',
    columnClass: 'text-left',
}, {
    id: 'status',
    title: '状态',
    width: 75,
    type: 'string',
    columnClass: 'text-center',
    resolution:function(){
        return "已經入庫";
    }
}, {
    id: 'belongto',
    title: '归属人',
    width: 75,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'belongProj',
    title: '归属项目',
    width: 75,
    type: 'string',
    columnClass: 'text-center',
}
];
